<?php

namespace app\langjinmobile\controller;
//导入基类
use think\Controller;
use think\facade\Cache;
use think\Db;
use app\common\model\Address as addressModel;
use think\Request;

class Merchandise extends Controller 
{
    // 上传产品
	public function recruitment(){
		return $this->fetch(); 
    }
    // 订单管理
    public function orderman(){
		return $this->fetch(); 
    }
    // 确认订单
    public function rightord(){
		return $this->fetch(); 
    }
     // 全部商品
    public function allproto(){
		  return $this->fetch(); 
    }

    // 地址管理
    public function addmar(){

        $uid = session('userid');
        //地址列表
        $add_list = addressModel::where('userid', $uid)->order('id', 'desc')->select();
        $this->assign('add_list', $add_list);
      return $this->fetch(); 
    }
    // // 地址编辑
    // public function addresseditor(){
    //   return $this->fetch(); 
    // }
    /**
     * 删除收货地址
     */

    public function del_address(Request $request)
    {
        $uid = session('userid');

        if ($request->isPost()) {
            $id = $request->param('id');
            $res = addressModel::where(['userid' => $uid, 'id' => $id])->find();
            if (empty($res)) {
                $data['status'] = 2;
                $data['msg'] = lang("删除失败");
            } else {
                if (addressModel::where(['userid' => $uid, 'id' => $id])->delete()) {
                    $data['status'] = 1;
                    $data['msg'] = lang("删除成功");
                } else {
                    $data['status'] = 2;
                    $data['msg'] = lang("删除失败");
                }
            }
            return $data;
        }

    }


    /**
     * 添加收货地址
     */

    public function addresseditor(Request $request)
    {
        $uid = session('userid');
        $order = session('shoporder');

        if ($request->isPost()) {
            $res = $request->param();
            if ($res['username'] == '' || $res['mobile'] == '' || $res['address'] == '') {
                $data['status'] = 2;
                $data['msg'] = lang("请填写完整信息");
            } else {
                $res['create_time'] = time();
                $res['userid'] = $uid;
                $status = addressModel::insert($res);
                if ($status) {
                    $data['status'] = 1;
                    $data['msg'] = lang("提交成功");
                } else {
                    $data['status'] = 2;
                    $data['msg'] = lang("提交失败");
                }
            }
            return $data;
        } else {
            $id = $request->param('id');
            $res = addressModel::where(['userid' => $uid, 'id' => $id])->find();
            $this->assign('res', $res);
            return $this->fetch();
        }

    }

    /**
     * 修改收货地址
     */

    public function edit_address(Request $request)
    {
        $uid = session('userid');
        if ($request->isPost()) {
            $res = $request->param();
            $result = addressModel::where(['userid' => $uid, 'id' => $res['id']])->find();
            if (empty($result)) {
                $data['status'] = 2;
                $data['msg'] = lang("信息异常");
                return $data;
            }
            if ($res['username'] == '' || $res['mobile'] == ''  || $res['address'] == '') {
                $data['status'] = 2;
                $data['msg'] = lang("请填写完整信息");
            } else {
                //$res['create_time'] = time();
                //$res['userid'] = $uid;
                $status = addressModel::where('id', $res['id'])->update([
                    'username' => $res['username'],
                    'address' => $res['address'],
                    'mobile' => $res['mobile']
                    // 'city' => $res['city'],
                    // 'area' => $res['area'],
                    // 'area' => $res['area'],
                    // 'street' => $res['street']
                ]);
                if ($status) {
                    $data['status'] = 1;
                    $data['msg'] = lang("修改成功");
                } else {
                    $data['status'] = 2;
                    $data['msg'] = lang("修改失败");
                }
            }
            return $data;
        }

    }

    /**
     * 默认收货地址
     */
    public function check_default(Request $request)
    {
        $uid = session('userid');
        $order = session('shoporder');

        if ($request->isPost()) {
            $id = $request->param('id');
            $res = addressModel::where(['userid' => $uid, 'id' => $id])->find();
            if ($res['attribute'] == 1) {
                $data['status'] = 3;
            } else {
                $data['attribute'] = 1;
                $res = addressModel::where('userid', $uid)->update(['attribute' => 0]);
                $update = addressModel::where(['userid' => $uid, 'id' => $id])->update($data);
                if ($update) {
                    $data['status'] = 1;
                    $data['msg'] = lang("设置默认地址成功");
                    if($order)
                    {
                        //确认订单过来的要跳回订单页
                        $data['url'] = url('Card/makesure');
                    }
                } else {
                    $data['status'] = 2;
                    $data['msg'] = lang("设置默认地址失败");
                }
            }
            return $data;
        }

    }


}